package com.javademo.asposewordjava;

import com.aspose.words.*;
import com.javademo.asposewordjava.utils.pdf.AsposeUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @program: AsposeWord-Java
 * @description: 动态生成Word文件
 * @author: Yonghao Ren
 * @create: 2022-12-05 16:20
 **/
public class AsPoseForJavaDemo {
    public static void doc2pdf(String inPath, String outPath) {
        FileOutputStream os = null;
        try {
            File file = new File(outPath); // 新建一个空白pdf文档
            os = new FileOutputStream(file);
            Document doc = new Document(inPath);


            doc.save(os, SaveFormat.PDF);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private final static String HOME = "D:\\AsposeWord-Java\\src\\main\\resources\\wordTemplates\\";
    public static void main(String[] args) throws Exception {
        AsposeUtil.getLicense(); //去除水印
        doc2pdf("d:/公司登记（备案）申请书.docx", "d:/公司登记（备案）申请书.pdf");
        Document doc = new Document();

        //在当前word下创建一个表格
        Table table = new Table(doc);
        //将表格附加至word文件中
        doc.getFirstSection().getBody().appendChild(table);


        //row是指当前单元格的第一列
        Row firstRow = new Row(doc);
        table.appendChild(firstRow);
        Row secondRow = new Row(doc);
        table.appendChild(secondRow);


        //cell指当前单元格某一行中的第几个单元格
        Cell firstCell1c1 = new Cell(doc);
        Cell secondCell1c2 = new Cell(doc);
        Cell firstCell2c1 = new Cell(doc);
        Cell secondCell2c2 = new Cell(doc);


        firstRow.appendChild(firstCell1c1);
        firstRow.appendChild(secondCell1c2);
        secondRow.appendChild(firstCell2c1);
        secondRow.appendChild(secondCell2c2);


        //paragraph是单元格内容中的段落
        Paragraph paragraph1t1 = new Paragraph(doc);
        firstCell1c1.appendChild(paragraph1t1);
        Paragraph paragraph1t2 = new Paragraph(doc);
        firstCell1c1.appendChild(paragraph1t2);
        Paragraph paragraph2t1 = new Paragraph(doc);
        secondCell1c2.appendChild(paragraph2t1);
        Paragraph paragraph3t1 = new Paragraph(doc);
        firstCell2c1.appendChild(paragraph3t1);
        Paragraph paragraph3t2 = new Paragraph(doc);
        secondCell2c2.appendChild(paragraph3t2);
        //run是指单个单元格中一个段落中的内容
        Run run1r1 = new Run(doc, "这是第一个单元格,第一行内容");
        paragraph1t1.appendChild(run1r1);
        Run run1r2 = new Run(doc, "这是第一个单元格，第二行内容");
        paragraph1t2.appendChild(run1r2);
        Run run2r2 = new Run(doc, "这是第二个单元格，第一行内容");
        paragraph2t1.appendChild(run2r2);
        Run run3r1 = new Run(doc, "这是第三个单元格，第一行内容");
        paragraph3t1.appendChild(run3r1);
        Run run3r2 = new Run(doc, "这是第四个单元格，第一行内容");
        paragraph3t2.appendChild(run3r2);
        SaveOutputParameters save = doc.save(HOME + "表格测试.docx");
    }
}
